<?php session_start();?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="maintenancePagesCSS.css" />
        <title></title>
    </head>
<?php

include "Customer.php";
include "CustomerDao.php";
include "Datasource.php";

$searchBox = $_POST["searchBox"];
$searchCriteria = $_POST["searchCriteria"];
$tempCustomer = new Customer();
$customerDao = new CustomerDao();
$conn = new Datasource("localhost","theanykey","root","");

switch ($searchCriteria)
{
    case "CustomerID":
        $tempCustomer->setCustomerID($searchBox);
        break;
    case "LastName":
        $tempCustomer->setLastName($searchBox);
        break;
    case "FirstName":
        $tempCustomer->setFirstName($searchBox);
        break;
}

$results = $customerDao->searchMatching($conn, $tempCustomer);

?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="maintenancePagesCSS.css" />
        <title></title>
    </head>
    <body>
        <center>
            <form method="post" action="CustomerPopupSearchFunc.php" id="CustomerSearchForm">
                <table class="EditTable">
                    <thead>
                        <tr>
                            <th colspan="2">Customer Search</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="searchBox" value="<?php print($searchBox)?>" size="25" id="searchBox"/></td>
                            <td>
                                <select name="searchCriteria" id="searchCriteria">
                                    <option value="CustomerID">Customer ID</option>
                                    <option value="LastName">Last Name</option>
                                    <option value="FirstName">First Name</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td><input type="button" value="Search" onclick = checkForm(); /></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </form>


        </center>
        <?php
        // put your code here
        if($searchBox != "")
        {
        print("<center><h3>Search Results</h3>");
        print("<table class='MenuDiv2'>
                    <thead>
                        <tr>
                            <th></th>
                            <th>Customer ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Address</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Zip</th>
                            <th>Phone 1</th>
                            <th>Phone 2</th>
                            <th>Phone 3</th>
                            <th>Email</th>
                        </tr>
                    </thead>
                    <tbody>");
        
        foreach ($results as $temp)
        {
            print("<tr>
                        <td><a href='CustomerPost.php?id=".$temp->getCustomerID()."'>Add</td>
                        <td>".$temp->getCustomerID()."</td>
                        <td>".$temp->getLastName()."</td>
                        <td>".$temp->getFirstName()."</td>
                        <td>".$temp->getAddress()."</td>
                        <td>".$temp->getCity()."</td>
                        <td>".$temp->getState()."</td>
                        <td>".$temp->getZip()."</td>
                        <td>".$temp->getPhoneNumber1()."</td>
                        <td>".$temp->getPhoneNumber2()."</td>
                        <td>".$temp->getPhoneNumber3()."</td>
                        <td>".$temp->getEmail()."</td>
                  </tr>");
    
        }

        print("</tbody></table></center>");

        }

        ?>
        
    </body>
    <script type="text/javascript">
        function checkForm()
        {
            if((document.getElementById("searchBox").value == ""))
            {
                alert("Search field is blank.");
            }
            else
            {
                document.getElementById("CustomerSearchForm").submit();
            }
        }

    </script>
</html>
